﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EvoAlgLib
{
    public class TournamentSelector
    {

        public List<Individual> select(List<Individual> pop)
        {
            List<Individual> res = new List<Individual>();
            for (int i = 0; i < 2; i++)
            {
                Individual best = null;
                double bestFitness = 0;
                for (int j = 0; j < 2; j++)
                {
                    int r = LibStatics.getRandomizer().Next(pop.Count());
                    double fitness = pop.ElementAt(r).getFitness();
                    if (best == null || fitness > bestFitness)
                    {
                        Individual tmp = pop.ElementAt(r);
                        if (best != null)
                            pop.Remove(best);

                        best = tmp;
                        bestFitness = fitness;
                    }
                    else
                    {
                        pop.RemoveAt(r);
                    }
                }
                res.Add(best);
            }
            return res;
        }
    }
}
